/*
 *  Copyright 2020-2025 the original author or authors.
 *  You cannot use this file unless authorized by the author.
 */

package org.ipig.calc.demo;

import org.apache.commons.lang3.StringUtils;
import org.ipig.proc.base.calc.CalcRule;
import org.ipig.proc.base.rule.RuleBuilder;
import org.ipig.proc.base.rule.RuleUnit;
import org.ipig.proc.fun.obj.DistinctedObj;

import java.util.HashMap;
import java.util.Map;

/**
 * DistinctedObj案例
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: DistinctedObjDemo.java 14 2020-04-17 06:56:19Z ts <comchnts@163.com> $$
 * @since 1.0
 */
public class DistinctedObjDemo implements CalcRule {
    private static Map<String, String> orgMap = new HashMap<>();

    static {
        orgMap.put("AAA", "AAA公司");
        orgMap.put("BBB", "BBB公司");
        orgMap.put("CCC", "CCC公司");
        orgMap.put("DDD", "DDD公司");
    }

    @Override
    public RuleUnit createCalcRule(RuleBuilder ruleBuilder) {
        return ruleBuilder.newInstance("每日的单量")
                .selectModel("DEMO0001", "IPIG", true)
                .selectKey((model) -> {
                    return "DEMO0001";
                })
                .selectTime((model) -> {
                    return model.getTime();
                })
                .selectMetrics("entryQty") //度量指标字段
                .filter((model) -> {
                    boolean flag = false;
                    if (StringUtils.isNoneBlank(orgMap.get(model.getValue("agentCode")))) {
                        flag = true;
                    }
                    return flag;
                })
                .expirePattern("10pd")
                .action((model) -> {
                    return new DistinctedObj(model.getValue("entryId")); //单号去重复
                }).build();
    }
}

